/*******************************************************************************
 * Copyright (c) 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//最后点击的分类控件(select)
var lastRegionSelect=null;
$(function(){
	lastRegionSelect=$("#region1")[0];
});
var arrayLevel=['国家','省/直辖市','地级市','县'];

//清空所有子类的显示
function clearChildRegion(level){
	for(var lv=level;lv<=4;lv++){
		var region=$("#region"+lv)[0];
		fnRemoveAllOptions(region);
		fnAddOption(region,"","----------"+arrayLevel[lv-1]+"级分类----------");	
	}
}
function fillChildRegion(obj,level){
		//当前的分类级别
	var thisLevel=$(obj).attr("level");
	
	//若直接选择下一级分类，而没有选择父类，则需要提示
	clearChildRegion(level);
	
	var fatherId=null;
	if(obj.value==''&& thisLevel >1){
		var fatherLevel=parseInt(thisLevel)-1;
		var fatherSelect=$("#region"+fatherLevel)[0];
		var isSelected=false;
		for(var cn=0;cn<fatherSelect.options.length;cn++){
			if(fatherSelect.options[cn].selected&&
			fatherSelect.options[cn].value!=''){
				fatherId=fatherSelect.options[cn].value;
				isSelected=true;
				break;
			}
		}
		if(!isSelected){
			alert('请选择上一级地区～～');
			return;
		}
	}
	
	//设置显示当前的分类标签
	$("#spanLevel").html(arrayLevel[thisLevel-1]);
	//设置当前选择的分类select控件对象
	lastRegionSelect=obj;
	
	//重置分类和子分类的添加表单
	$("#fatherForm")[0].reset();
	$("#childForm")[0].reset();
	$("#parentId").val(fatherId);
	
	//若分类>4，则返回，并且子类的添加界面关闭
	if(level>4||obj.value==''){
		$("#childForm").css('display','none');
		return;
	}else{
		$("#childForm").css('display','');
	}
	
	if(obj.value!=""){
		//填充该分类下的所有子类至下一级分类选择框
		regionManager.getByParentId(obj.value,function(data){
			var sourceObj=$("#region"+level)[0];
			for(var i=0;i<data.length;i++){
				fnAddOption(sourceObj,data[i].regionId,data[i].regionName);
			}
		});
		//加载当前选择的分类信息
		regionManager.getById(obj.value,function(data){
			$("#regionName").val(data.regionName);
			$("#regionType").val(data.regionType);
			$("#regionCode").val(data.regionCode);
			$("#zipCode").val(data.zipCode);
			$("#regionIcon").val(data.regionIcon);
			$("#regionId").val(data.cateId);
			$("#parentId").val(data.parentId);
			//设置其子类的父Id
			$("#_parentId").val(data.regionId);
			if(data.status==1){
				$("#status").attr("checked",true);
			}else{
				$("#status").attr("checked",false);
				}
			});
		}
}
/**
  当flag='_'时，则表示为子类添加或更新数据
**/
function saveOrUpdateRegion(flag){
	alert("test0");
	var regionName=$("#"+flag+"regionName").val();
	var regionCode=$("#"+flag+"regionCode").val();
	var regionType=$("#"+flag+"regionType").val();
	var zipCode=$("#"+flag+"zipCode").val();
	var regionIcon=$("#"+flag+"regionIcon").val();
	var regionId=$("#"+flag+"regionId").val();
	var parentId=$("#"+flag+"parentId").val();
	var ckStatus=$("#"+flag+"status")[0];
	var status=0;
	/*if(sortOrder==""){
		sortOrder=null;
	}*/
	if(ckStatus.checked){
		status=1;
	}
	if(regionId==""){
		categoryId=null;
	}
	if(parentId==""){
		parentId=null;
	}
	if(zipCode==""){
		zipCode=null;
	}
	if(regionIcon==""){
		regionIcon=null;
	}
	if(regionType){
		regionType=null;
	}
	//-----------------------------
	//validate the data here [TODO]
	
	if(regionName.trim()==""){
		alert("区或名称为必填！");
		$("#"+flag+"regionName").focus();
		return;
	}
	if(regionName.length>128){
		alert("区域名称不能超过64个字符！");
		$("#"+flag+"regionName").focus();
		return;
	}
	if(regionCode.trim()==""){
		alert("区域编码为必填！");
		$("#"+flag+"regionCode").focus();
		return;
	}
	/*if(regionType.trim()==""){
		alert("区域类型为必填！");
		$("#"+flag+"regionType").focus();
		return;
	}*/
	if(regionCode.length>48){
		alert("区域编码不能超过48个字符！");
		$("#"+flag+"regionCode").focus();
		return;
	}
	/*var exPattern=/[0-9]*[1-9][0-9]*$/;
	if(!exPattern.test(sortOrder)){
		alert("输入的排序号不正确！");
		$("#"+flag+"sortOrder").focus();
		return;
	}*/
	alert("test1");
	//同步
	var isExist=false;
	regionManager.getByCode(regionCode,{async:false,callback:function(data){
		if(data!=null&&data.regionId!=regionId){
			alert("该分类编码已经存在,请输入其他编码");
			isExist=true;
		}
	}});
	if(isExist)return;
	
	//----------------------------
	var targetSelect=lastRegionSelect;
	if(flag=='_'){
		alert("_");
		var level=$(lastRegionSelect).attr('level');
		nextLevel=parseInt(level)+1;
		targetSelect=$('#cat'+nextLevel)[0];
	}

	regionManager.saveRegion(regionId,regionName,regionCode,zipCode,regionIcon,regionType,status,parentId,
		function(data){
			alert('保存成功');
			//添加分类
			if(regionId==null){
				alert("the regionId=null");
				targetSelect.options[targetSelect.options.length]=new Option(data.regionName,data.regionId);
				
			}else{//更新分类
				alert("the regionId!=null");
				for(var i=0;i<targetSelect.options.length;i++){
					if(targetSelect.options[i].value==regionId){
						targetSelect.options[i]=new Option(regionName,regionId);
						break;
					}
				}
			}
			$("#fatherForm")[0].reset();
			//保留parentId以便下一次添加
			$("#parentId").val(parentId)
			$("#childForm")[0].reset();
		});
}

function deleteRegion(){
	var regionId=$("#regionId").val();
	//var lastRegionSelect
	regionManager.deleteById(regionId,function(){
		for(var i=0;i<lastCatSelect.options.length;i++){
			if(lastCatSelect.options[i].value==regionId){
				lastCatSelect.options[i]=null;
				break;
			}
		}
		
		var level=$(lastRegionSelect).attr('level');
		level=parseInt(level)+1;
		$("#childForm").css('display','none');
		if(level>5){
			return;	
		}
		clearChildRegion(level)
		$("#fatherForm")[0].reset();
		alert("成功删除该分类!");
	});
}